﻿using System;
using System.ComponentModel;
using System.Text.RegularExpressions;

namespace CalculoPreciosUnitarios.Logica.GestorObject.Articulo
{
    public class ArticuloCS : INotifyPropertyChanged, IDataErrorInfo
    {
        private string codigo;
        private string detalle;
        private decimal precio;
        private IFormatProvider config_regional = new System.Globalization.CultureInfo("es-AR");
        public event PropertyChangedEventHandler PropertyChanged;

        public string Codigo
        {
            get
            {
                return codigo;
            }
            set
            {                
                codigo = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Codigo"));
            }
        }

        public string Detalle
        {
            get
            {
                return detalle;
            }
            set
            {
                detalle = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Detalle"));
            }
        }

        public decimal Precio
        {
            get
            {
                return precio;
            }
            set
            {
                //precio = Convert.ToDecimal(value, english);         
                precio = value;
                if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("Precio"));
            }
        }

        public ArticuloCS()
        {
            Codigo = "";
            Detalle = "";
            Precio = 0;
        }

        public string Error
        {
            get
            {
                return null;
            }
        }

        public string this[string propertyName]
        {
            get
            {
                return IsValid(propertyName);
            }
        }

        private string IsValid(string propertyName)
        {
            switch (propertyName)
            {
                case "Codigo":
                    if (string.IsNullOrWhiteSpace(Codigo))
                        return "El campo Codigo es requerido - ej: 1Ax3";
                    else
                    {
                        Regex regEx;
                        regEx = new Regex(@"^[0-9a-zA-Z]{1,4}$");
                        if (!regEx.IsMatch(Codigo))
                            return "El campo Codigo no " +
                                   "tiene un formato correcto \n ej:1Ax3";
                    }
                    break;

                case "Detalle":
                    if (string.IsNullOrWhiteSpace(Detalle))
                        return "El campo detalle del artículo es requerido";
                    break;

                case "Precio":
                    if (string.IsNullOrWhiteSpace(Precio.ToString()))
                        return "El campo precio es requerido \n ej: 23489,34";
                    else
                    {                             
                        Regex regEx;
                        regEx = new Regex(@"^\d+(\,?\d{0,2})?$");
                        if (!regEx.IsMatch(Precio.ToString()))
                            return "El campo precio no " +
                                   "tiene un formato correcto \n ej: 23489,34";
                    }
                    break;


            }
            return null;
        }

        public bool IsValid()
        {            
            return string.IsNullOrEmpty(IsValid("Codigo"))
                && string.IsNullOrEmpty(IsValid("Detalle"));
                //&& string.IsNullOrEmpty(IsValid("Precio"));           
        }
    }
}
